package com.system.manager.modules.development.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.system.manager.common.entity.Result;
import com.system.manager.common.exception.SysException;
import com.system.manager.modules.development.entity.DevePurchase;
import com.system.manager.modules.development.entity.DeveRecord;
import com.system.manager.modules.development.mapper.DevePurchaseMapper;
import com.system.manager.modules.development.service.IDevePurchaseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yxp
 * @since 2021-11-17
 */
@Service
public class DevePurchaseServiceImpl extends ServiceImpl<DevePurchaseMapper, DevePurchase> implements IDevePurchaseService {

    @Autowired
    private DevePurchaseMapper devePurchaseMapper;

    /**
     * TODO 新建采购报告
     *
     * @param devePurchase
     * @return
     */
    @Override
    public void insertPurchase(DevePurchase devePurchase) {
        try {
            System.out.println("----------");
            System.out.println(devePurchase);
            System.out.println("----------");

            devePurchaseMapper.insert(devePurchase);
        }catch (Exception e){
            throw new SysException();
        }
    }

    /**
     * TODO 删除报告
     * @param id
     */

    public void deletePurchase(Integer id){
        try{
            devePurchaseMapper.deleteById(id);
        }catch (Exception e){
            throw  new SysException();
        }
    }

    /**
     * 查找报告
     * @param id
     * @return
     */

    @Override
    public Result selectAllPurchase(Integer page, Integer size, Integer id) {
        IPage<DevePurchase> iPage = new Page<>(page,size);

        QueryWrapper<DevePurchase> queryWrapper = new QueryWrapper<>();
        if(id != null){
            queryWrapper.eq("id",id);
        }
        //分页查询
        page(iPage,queryWrapper);
        List<DevePurchase> records = iPage.getRecords();
        int total =(int) iPage.getTotal();
        return Result.ok(records,total);
    }
}
